﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Markup;
using XLib.Base.Ex;
using XLib.Node;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace NodeLib.Sample
{
    public class FuncDataPinGroup : DataPinGroup
    {
        public FuncDataPinGroup(NodeBase node) : base(node) { }

        public FuncDataPinGroup(NodeBase node, string type, string name, string value) : base(node, type, name, value) { }

        public FuncDataPinGroup(NodeBase node, string name)
            : base(node)
        {
            this.GroupType = PinGroupType.Control;
            this.Type = "CustomFunc";
            this.Name = name;
        }

        private static Dictionary<string, Func<bool>> _funcs
            = new Dictionary<string, Func<bool>>();

        public static Dictionary<string, ObservableCollection<string>> GroupFuncsDict
            = new Dictionary<string, ObservableCollection<string>>();

        public static ObservableCollection<string> _GroupNames
            = new ObservableCollection<string>();

        public override object Value
        {
            set
            {
                _value = value;
                var tjoken = value as JToken;
                if (tjoken != null)
                {
                    var list = tjoken.ToObject<List<string>>();
                    if (list.Count >= 2)
                    {
                        SelectedFuncName = list[0] as string;
                        SelectedGroupName = list[1] as string;
                    }
                }
            }
            get
            {
                List<string> list = new List<string> {
                    SelectedFuncName,
                    SelectedGroupName
                };
                _value = list;
                return _value;
            }
        }

        private string _SelectedGroupName = "";
        public string SelectedGroupName
        {
            get => _SelectedGroupName;
            set
            {
                _SelectedGroupName = value;
                GroupFuncs = GroupFuncsDict[_SelectedGroupName];
            }
        }

        private string _SelectedFuncName = "";
        public string SelectedFuncName
        {
            get => _SelectedFuncName;
            set
            {
                _SelectedFuncName = value;
            }
        }
        public ObservableCollection<string> GroupNames
        {
            get
            {
                return FuncDataPinGroup._GroupNames;
            }
            set
            {
                FuncDataPinGroup._GroupNames = value;
                OnPropertyChanged("GroupNames");
            }
        }

        public ObservableCollection<string> GroupFuncs
        {
            get
            {
                bool result = FuncDataPinGroup.GroupFuncsDict.TryGetValue(_SelectedGroupName, out ObservableCollection<string> findresult);
                if (result)
                {
                    return findresult;
                }
                else
                {
                    return new ObservableCollection<string>();
                }
            }
            set
            {
                FuncDataPinGroup.GroupFuncsDict[_SelectedGroupName] = value;
                OnPropertyChanged("GroupFuncs");
            }
        }

        [DisplayName("空函数"), Category("函数")]
        public static bool EmptyFunc()
        {
            Trace.WriteLine("FuncDataPinGroup.EmptyFunc()");
            return true;
        }
        [DisplayName("总是成功"), Category("函数")]
        public static bool AlwaysSuccess()
        {
            Trace.WriteLine("FuncDataPinGroup.AlwaysSuccess()");
            return true;
        }
        [DisplayName("总是失败"), Category("函数")]
        public static bool AlwaysFailed()
        {
            Trace.WriteLine("FuncDataPinGroup.AlwaysFailed()");
            return true;
        }

        public static void RegisterFuncs<T>() where T : class
        {
            //获取所有静态方法
            var methods = typeof(T).GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
            foreach (var method in methods)
            {
                if (method.ReturnType == typeof(bool))
                {
                    //获取方法的DisplayName特性
                    string dispname = method.GetCustomAttributes(typeof(DisplayNameAttribute), false).FirstOrDefault() is DisplayNameAttribute attr ? attr.DisplayName : method.Name;
                    _funcs.Add(dispname, () => (bool)method.Invoke(null, null));
                    if (!GroupFuncsDict.ContainsKey("函数"))
                    {
                        GroupFuncsDict.Add("函数", new ObservableCollection<string>());
                    }
                    GroupFuncsDict["函数"].Add(dispname);
                }
            }
            FuncDataPinGroup._GroupNames = new ObservableCollection<string>(GroupFuncsDict.Keys);

        }

        public bool RunFunc(string funcname)
        {
            if (funcname == "")
                return true;
            var result = _funcs.TryGetValue(funcname, out Func<bool> func);
            if (result)
            {
                return func();
            }
            return true;
        }

    }
}
